import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/Layout.vue'
import Cookies from "js-cookie";
import userLayout from "@/views/userPage/userLayout.vue";
Vue.use(VueRouter)

const routes = [
    //  ====== 登录  =====
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/Login.vue'),
    },
    {
        path: '/userlogin',
        name: 'userLogin',
        component: () => import('@/views/userPage/LoginUser.vue'),
    },
    {
        path: '/views',
        name: 'Views',
        component: () => import('@/views/echarts/MyEcharts.vue'),
    },
    //用户主页
    {
        path: '/',
        name: 'userLayout',
        component: userLayout,
        redirect: '/personalInformation',
        children: [
            {
                path: '/personalInformation',
                name: 'PersonalInformation',
                component: () => import('@/views/userPage/personalInformation.vue'),
            },
            {
                path: '/userBorrowList',
                name: 'userBorrowList',
                component: () => import('@/views/userPage/userBorrowList.vue'),
            },
            {
                path: '/userList',
                name: 'UserBookList',
                component: () => import('@/views/userPage/userList.vue'),
            },
            {path: '/echarts', name: 'MyEchartsU', component: () => import("@/views/echarts/MyEcharts.vue")},
        ]
    },
    //  ====== 管理员主页  =====
    {
        path: '/admin',
        name: 'Layout',
        component: Layout,
        redirect: '/admin/home',
        children: [  // 子路由
            {
                path: 'home',
                name: 'Home',
                component: () => import('@/views/home/HomeView.vue'),
            },
            //  ====  User  ====
            {
                path: 'userList',
                name: 'UserList',
                component: () => import('@/views/user/User.vue'),
            },
            {
                path: 'addUser',
                name: 'AddUser',
                component: () => import('@/views/user/AddUser.vue'),
            },
            {
                path: 'user',
                name: 'User',
                component: () => import('@/views/user/User.vue'),
            },
            {
                path: 'editUser',
                name: 'EditUser',
                component: () => import('@/views/user/EditUser.vue'),
            },
            //  ====  Admin  ====
            {
                path: 'adminList',
                name: 'AdminList',
                component: () => import('@/views/admin/List.vue'),
            },
            {
                path: 'addAdmin',
                name: 'AddAdmin',
                component: () => import('@/views/admin/Add.vue'),
            },
            //  ====  Book  ====
            {path: 'bookList', name: 'BookList', component: () => import('@/views/book/List.vue')},
            {path: 'addBook', name: 'AddBook', component: () => import('@/views/book/Add.vue')},
            {path: 'editBook', name: 'EditBook', component: () => import('@/views/book/Edit.vue')},
            //  ====  Borrow  ====
            {path: 'borrowList', name: 'BorrowList', component: () => import('@/views/borrow/List.vue')},
            {path: 'addBorrow', name: 'AddBorrow', component: () => import('@/views/borrow/Add.vue')},
            // 上传文件
            {path: 'uploadFile', name: 'uploadFile', component: () => import('@/views/borrow/uploadFile.vue')},
            // 数据展示
            {path: 'echarts', name: 'MyEcharts', component: () => import("@/views/echarts/MyEcharts.vue")}
        ]
    },
    {
        path: "*",
        component: () => import('@/views/404')
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if(to.path === '/views') next()
    if (to.path === '/login') next()// 如果是访问登录页面，直接放行
    if (to.path === '/userlogin') next()// 如果是访问登录页面，直接放行
    const user = Cookies.get("user")// 获取cookie里面的数据
    const admin = Cookies.get("admin")// 获取cookie里面的数据
if(['/personalInformation','/userBorrowList','/userList','/echarts'].includes(to.path)){
        if(!user) return next("/userlogin")// 强制退回到登录页面
    }
    if(!admin&&to.path!=='/login'&&to.path!=='/userlogin'&&!['/personalInformation','/userBorrowList','/userList','/echarts'].includes(to.path)){
       return next("/login")// 强制退回到登录页面
    }
    next()
})

export default router
